function wordsToBytes(e) {
  for (var t = [], r = 0; r < 32 * e.length; r += 8)
    t.push((e[r >>> 5] >>> (24 - (r % 32))) & 255);
  return t;
}
function stringToBytes(e) {
  return istringToBytes(unescape(encodeURIComponent(e)));
}
function istringToBytes(e) {
  for (var t = [], r = 0; r < e.length; r++) t.push(255 & e.charCodeAt(r));
  return t;
}

function bytesToWords(e) {
  for (var t = [], r = 0, n = 0; r < e.length; r++, n += 8)
    t[n >>> 5] |= e[r] << (24 - (n % 32));
  return t;
}
function _ff(e, t, r, n, i, o, a) {
  var s = e + ((t & r) | (~t & n)) + (i >>> 0) + a;
  return ((s << o) | (s >>> (32 - o))) + t;
}
function _gg(e, t, r, n, i, o, a) {
  var s = e + ((t & n) | (r & ~n)) + (i >>> 0) + a;
  return ((s << o) | (s >>> (32 - o))) + t;
}
function _hh(e, t, r, n, i, o, a) {
  var s = e + (t ^ r ^ n) + (i >>> 0) + a;
  return ((s << o) | (s >>> (32 - o))) + t;
}
function _ii(e, t, r, n, i, o, a) {
  var s = e + (r ^ (t | ~n)) + (i >>> 0) + a;
  return ((s << o) | (s >>> (32 - o))) + t;
}
function rotl(e, t) {
  return (e << t) | (e >>> (32 - t));
}

function endian(e) {
  if (e.constructor == Number)
    return (16711935 & rotl(e, 8)) | (4278255360 & rotl(e, 24));
  for (var t = 0; t < e.length; t++) e[t] = endian(e[t]);
  return e;
}

function md52(t, r) {
  t.constructor == String
    ? (t =
        r && "binary" === r.encoding ? bin.stringToBytes(t) : stringToBytes(t))
    : isBuffer2(t)
    ? (t = Array.prototype.slice.call(t, 0))
    : Array.isArray(t) || t.constructor === Uint8Array || (t = t.toString());
  for (
    var n = bytesToWords(t),
      i = 8 * t.length,
      o = 1732584193,
      a = -271733879,
      s = -1732584194,
      l = 271733878,
      c = 0;
    c < n.length;
    c++
  )
    n[c] =
      (16711935 & ((n[c] << 8) | (n[c] >>> 24))) |
      (4278255360 & ((n[c] << 24) | (n[c] >>> 8)));
  (n[i >>> 5] |= 128 << i % 32), (n[14 + (((i + 64) >>> 9) << 4)] = i);
  var u = _ff,
    d = _gg,
    p = _hh,
    h = _ii;
  for (c = 0; c < n.length; c += 16) {
    var f = o,
      m = a,
      v = s,
      g = l;
    (o = u(o, a, s, l, n[c + 0], 7, -680876936)),
      (l = u(l, o, a, s, n[c + 1], 12, -389564586)),
      (s = u(s, l, o, a, n[c + 2], 17, 606105819)),
      (a = u(a, s, l, o, n[c + 3], 22, -1044525330)),
      (o = u(o, a, s, l, n[c + 4], 7, -176418897)),
      (l = u(l, o, a, s, n[c + 5], 12, 1200080426)),
      (s = u(s, l, o, a, n[c + 6], 17, -1473231341)),
      (a = u(a, s, l, o, n[c + 7], 22, -45705983)),
      (o = u(o, a, s, l, n[c + 8], 7, 1770035416)),
      (l = u(l, o, a, s, n[c + 9], 12, -1958414417)),
      (s = u(s, l, o, a, n[c + 10], 17, -42063)),
      (a = u(a, s, l, o, n[c + 11], 22, -1990404162)),
      (o = u(o, a, s, l, n[c + 12], 7, 1804603682)),
      (l = u(l, o, a, s, n[c + 13], 12, -40341101)),
      (s = u(s, l, o, a, n[c + 14], 17, -1502002290)),
      (o = d(
        o,
        (a = u(a, s, l, o, n[c + 15], 22, 1236535329)),
        s,
        l,
        n[c + 1],
        5,
        -165796510
      )),
      (l = d(l, o, a, s, n[c + 6], 9, -1069501632)),
      (s = d(s, l, o, a, n[c + 11], 14, 643717713)),
      (a = d(a, s, l, o, n[c + 0], 20, -373897302)),
      (o = d(o, a, s, l, n[c + 5], 5, -701558691)),
      (l = d(l, o, a, s, n[c + 10], 9, 38016083)),
      (s = d(s, l, o, a, n[c + 15], 14, -660478335)),
      (a = d(a, s, l, o, n[c + 4], 20, -405537848)),
      (o = d(o, a, s, l, n[c + 9], 5, 568446438)),
      (l = d(l, o, a, s, n[c + 14], 9, -1019803690)),
      (s = d(s, l, o, a, n[c + 3], 14, -187363961)),
      (a = d(a, s, l, o, n[c + 8], 20, 1163531501)),
      (o = d(o, a, s, l, n[c + 13], 5, -1444681467)),
      (l = d(l, o, a, s, n[c + 2], 9, -51403784)),
      (s = d(s, l, o, a, n[c + 7], 14, 1735328473)),
      (o = p(
        o,
        (a = d(a, s, l, o, n[c + 12], 20, -1926607734)),
        s,
        l,
        n[c + 5],
        4,
        -378558
      )),
      (l = p(l, o, a, s, n[c + 8], 11, -2022574463)),
      (s = p(s, l, o, a, n[c + 11], 16, 1839030562)),
      (a = p(a, s, l, o, n[c + 14], 23, -35309556)),
      (o = p(o, a, s, l, n[c + 1], 4, -1530992060)),
      (l = p(l, o, a, s, n[c + 4], 11, 1272893353)),
      (s = p(s, l, o, a, n[c + 7], 16, -155497632)),
      (a = p(a, s, l, o, n[c + 10], 23, -1094730640)),
      (o = p(o, a, s, l, n[c + 13], 4, 681279174)),
      (l = p(l, o, a, s, n[c + 0], 11, -358537222)),
      (s = p(s, l, o, a, n[c + 3], 16, -722521979)),
      (a = p(a, s, l, o, n[c + 6], 23, 76029189)),
      (o = p(o, a, s, l, n[c + 9], 4, -640364487)),
      (l = p(l, o, a, s, n[c + 12], 11, -421815835)),
      (s = p(s, l, o, a, n[c + 15], 16, 530742520)),
      (o = h(
        o,
        (a = p(a, s, l, o, n[c + 2], 23, -995338651)),
        s,
        l,
        n[c + 0],
        6,
        -198630844
      )),
      (l = h(l, o, a, s, n[c + 7], 10, 1126891415)),
      (s = h(s, l, o, a, n[c + 14], 15, -1416354905)),
      (a = h(a, s, l, o, n[c + 5], 21, -57434055)),
      (o = h(o, a, s, l, n[c + 12], 6, 1700485571)),
      (l = h(l, o, a, s, n[c + 3], 10, -1894986606)),
      (s = h(s, l, o, a, n[c + 10], 15, -1051523)),
      (a = h(a, s, l, o, n[c + 1], 21, -2054922799)),
      (o = h(o, a, s, l, n[c + 8], 6, 1873313359)),
      (l = h(l, o, a, s, n[c + 15], 10, -30611744)),
      (s = h(s, l, o, a, n[c + 6], 15, -1560198380)),
      (a = h(a, s, l, o, n[c + 13], 21, 1309151649)),
      (o = h(o, a, s, l, n[c + 4], 6, -145523070)),
      (l = h(l, o, a, s, n[c + 11], 10, -1120210379)),
      (s = h(s, l, o, a, n[c + 2], 15, 718787259)),
      (a = h(a, s, l, o, n[c + 9], 21, -343485551)),
      (o = (o + f) >>> 0),
      (a = (a + m) >>> 0),
      (s = (s + v) >>> 0),
      (l = (l + g) >>> 0);
  }
  return endian([o, a, s, l]);
}
function bytesToHex(e) {
  for (var t = [], r = 0; r < e.length; r++)
    t.push((e[r] >>> 4).toString(16)), t.push((15 & e[r]).toString(16));
  return t.join("");
}

function md5(e, t = undefined) {
  console.log("e", e);
  if (null == e) throw new Error("Illegal argument " + e);
  var r = wordsToBytes(md52(e, t));
  return t && t.asBytes
    ? r
    : t && t.asString
    ? bin.bytesToString(r)
    : bytesToHex(r);
}
function formatImgByLocalParams(e, t) {
  t || (t = {});

  var r = getImgFormatConfig(t),
    n = r.imgKey,
    i = r.subKey;

  if (n && i) {
    for (
      var o = getPictureHashKey(n + i),
        a = Math.round(Date.now() / 1e3),
        s = Object.assign({}, e, {
          wts: a,
        }),
        l = Object.keys(s).sort(),
        c = [],
        u = /[!'()*]/g,
        d = 0;
      d < l.length;
      d++
    ) {
      var p = l[d],
        h = s[p];
      h && "string" == typeof h && (h = h.replace(u, "")),
        null != h &&
          c.push(
            "".concat(encodeURIComponent(p), "=").concat(encodeURIComponent(h))
          );
    }
    var f = c.join("&");

    return {
      w_rid: md5(f + o),
      wts: a.toString(),
    };
  }
  return null;
}
function getImgFormatConfig(e) {
  var t;
  if (e.useAssignKey)
    return {
      imgKey: e.wbiImgKey,
      subKey: e.wbiSubKey,
    };
  var r = [
    "https://i0.hdslb.com/bfs/wbi/7cd084941338484aae1ad9425b84077c.png",

    "https://i0.hdslb.com/bfs/wbi/4932caff0ff746eab6f01bf08b70ac45.png",
  ];
  (n = r[0]), (i = r[1]);
  return {
    imgKey: n ? getKeyFromURL(n) : e.wbiImgKey,
    subKey: i ? getKeyFromURL(i) : e.wbiSubKey,
  };
}
function getKeyFromURL(e) {
  return e.substring(e.lastIndexOf("/") + 1, e.length).split(".")[0];
}
LOCAL_STORAGE_KEY$1 = "wbi_img_urls";
function getLocal(e) {
  try {
    return localStorage.getItem(e);
  } catch (e2) {
    return null;
  }
}
function getPictureHashKey(e) {
  var t = [];
  return (
    [
      46, 47, 18, 2, 53, 8, 23, 32, 15, 50, 10, 31, 58, 3, 45, 35, 27, 43, 5,
      49, 33, 9, 42, 19, 29, 28, 14, 39, 12, 38, 41, 13, 37, 48, 7, 16, 24, 55,
      40, 61, 26, 17, 0, 1, 60, 51, 30, 4, 22, 25, 54, 21, 56, 59, 6, 63, 57,
      62, 11, 36, 20, 34, 44, 52,
    ].forEach(function (r) {
      e.charAt(r) && t.push(e.charAt(r));
    }),
    t.join("").slice(0, 32)
  );
}

const e = {
  web_location: "333.337",
};

const t = {
  wbiImgKey: "c458435a75b1419ca98ab6d88b4c60d4",
  wbiSubKey: "446140f6859f439e9dd83f7ef858d1cd",
};

const w_rid = formatImgByLocalParams(e, t)["w_rid"];
const wts = formatImgByLocalParams(e, t)["wts"];
console.log(formatImgByLocalParams(e, t));

async function fetchBilibiliData(w_rid, wts) {
  const params = new URLSearchParams({
    pn: "1",
    ps: "40",
    tid: "0",
    special_type: "",
    order: "click",
    mid: "232004597",
    index: "0",
    keyword: "",
    order_avoided: "true",
    platform: "web",
    web_location: "333.1387",
    dm_img_list:
      '[{"x":636,"y":710,"z":0,"timestamp":14,"k":114,"type":0},{"x":2325,"y":1465,"z":6,"timestamp":33,"k":114,"type":0},{"x":2894,"y":1946,"z":172,"timestamp":145,"k":108,"type":0},{"x":2755,"y":1748,"z":72,"timestamp":256,"k":74,"type":0},{"x":2959,"y":1976,"z":250,"timestamp":436,"k":99,"type":0},{"x":2942,"y":2273,"z":27,"timestamp":538,"k":82,"type":0},{"x":3385,"y":2723,"z":472,"timestamp":684,"k":105,"type":0},{"x":3306,"y":2644,"z":439,"timestamp":785,"k":67,"type":0},{"x":3169,"y":2323,"z":417,"timestamp":885,"k":92,"type":0},{"x":3070,"y":1821,"z":561,"timestamp":986,"k":67,"type":0},{"x":2865,"y":1234,"z":582,"timestamp":1087,"k":97,"type":0},{"x":2526,"y":497,"z":402,"timestamp":1187,"k":85,"type":0},{"x":2017,"y":-802,"z":78,"timestamp":1288,"k":63,"type":0},{"x":2325,"y":-947,"z":388,"timestamp":1389,"k":112,"type":0},{"x":3212,"y":94,"z":836,"timestamp":2093,"k":115,"type":0},{"x":3317,"y":288,"z":904,"timestamp":2198,"k":99,"type":0},{"x":3425,"y":549,"z":1059,"timestamp":2300,"k":116,"type":0},{"x":2883,"y":589,"z":726,"timestamp":2401,"k":117,"type":0},{"x":2341,"y":325,"z":339,"timestamp":2502,"k":79,"type":0},{"x":2742,"y":925,"z":879,"timestamp":2602,"k":114,"type":0},{"x":3294,"y":1609,"z":1518,"timestamp":2702,"k":99,"type":0},{"x":3105,"y":1438,"z":1344,"timestamp":2803,"k":103,"type":0}]',
    dm_img_str: "V2ViR0wgMS4wIChPcGVuR0wgRVMgMi4wIENocm9taXVtKQ",
    dm_cover_img_str:
      "QU5HTEUgKEludGVsLCBJbnRlbChSKSBJcmlzKFIpIFhlIEdyYXBoaWNzICgweDAwMDA5QTQ5KSBEaXJlY3QzRDExIHZzXzVfMCBwc181XzAsIEQzRDExKUdvb2dsZSBJbmMuIChJbnRlbC",
    dm_img_inter:
      '{"ds":[{"t":2,"c":"cmFkaW8tZmlsdGVyX19pdGVtIHJhZGlvLWZpbHRlcl9faXRlbS0tYWN0aX","p":[1490,96,880],"s":[229,555,666]}],"wh":[4175,5400,77],"of":[708,1254,465]}',
    w_webid:
      "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzcG1faWQiOiIzMzMuMTM4NyIsImJ1dmlkIjoiRDkzQzUyMDAtMkJGMi1FRkM4LTQ1RjEtRjFFNzY3MTg1MzdCNDE4OTJpbmZvYyIsInVzZXJfYWdlbnQiOiJNb3ppbGxhLzUuMCAoV2luZG93cyBOVCAxMC4wOyBXaW42NDsgeDY0KSBBcHBsZVdlYktpdC81MzcuMzYgKEtIVE1MLCBsaWtlIEdlY2tvKSBDaHJvbWUvMTM2LjAuMC4wIFNhZmFyaS81MzcuMzYgRWRnLzEzNi4wLjAuMCIsImJ1dmlkX2ZwIjoiY2Q2ODgwNTU2ZDg1YTUyY2ZjNjNiOTBiZjQwNjY2ZjMiLCJiaWxpX3RpY2tldCI6ImV5SmhiR2NpT2lKSVV6STFOaUlzSW10cFpDSTZJbk13TXlJc0luUjVjQ0k2SWtwWFZDSjkuZXlKbGVIQWlPakUzTkRneE5UYzNNalVzSW1saGRDSTZNVGMwTnpnNU9EUTJOU3dpY0d4MElqb3RNWDAuYXBRS2J6TF9WekR2SEdwNmpjWkVCTVFoZkRreXNCN2JBYmxVVmFSUE1ZSSIsImNyZWF0ZWRfYXQiOjE3NDgwMTAyMjcsInR0bCI6ODY0MDAsInVybCI6Ii8yMzIwMDQ1OTcvdXBsb2FkL3ZpZGVvIiwicmVzdWx0IjowLCJpc3MiOiJnYWlhIiwiaWF0IjoxNzQ4MDEwMjI3fQ.LHWNJl0gZJbI7gpXKnBwOAqer-tKZ5OGKImQ-Q4wJJE6x2lDgV7WZ_OXG7yEjlQ3L6h8h2Tuq8e3xgr3WYbq8Wyjllc3blvhc2m3BOWOYikqa_Xhhb7yvpMEdOyiaGfYgxaGe-cAEkKFWWkGiipCeBpSMoBSROG8wErZt92x2cZ9ws9YFE0H7OOQZ7frmZpZOWk4wPgA2wicVmDdjuDUN_gBcp7xg_xeIirM9gu5rvzZkUuNAPze-nrmFRredM9gDEw7DboCzhKnhrTzxikhGheN8TapFMNIJHKEznI1MxmGanYjqZYi7owtfBdazkpS1Is9TMffKU9mU9hfikXyiQ",
    w_rid: w_rid,
    wts: wts,
  });

  const response = await fetch(
    `https://api.bilibili.com/x/space/wbi/arc/search?${params}`,
    {
      method: "GET",
      headers: {
        "User-Agent":
          "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 Edg/136.0.0.0",
        Referer: "https://space.bilibili.com/232004597/video",
        Origin: "https://space.bilibili.com",
      },
    }
  );

  if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
  return await response.json();
}

// 使用示例
fetchBilibiliData(w_rid, wts)
  .then((data) => console.log(data))
  .catch((error) => console.error("Error:", error));
